package io.aircraft.modules.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.aircraft.common.exception.RootException;
import io.aircraft.common.page.PageData;
import io.aircraft.common.utils.ConvertUtils;
import io.aircraft.modules.sys.dto.SysUserDTO;
import io.aircraft.modules.sys.service.SysUserService;
import io.aircraft.modules.wms.entity.SpuInfoEntity;
import io.aircraft.modules.wms.service.SpuInfoService;
import io.aircraft.common.service.impl.CrudServiceImpl;
import io.aircraft.modules.wms.dao.SpuInfoDao;
import io.aircraft.modules.wms.dto.SpuInfoDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * spu信息
 *
 * @author Cycle cycle@gmail.com
 * @since 1.0.0 2022-10-04
 */
@Service
public class SpuInfoServiceImpl extends CrudServiceImpl<SpuInfoDao, SpuInfoEntity, SpuInfoDTO> implements SpuInfoService {
    @Autowired
    private SysUserService sysUserService;

    @Override
    public PageData<SpuInfoDTO> page(Map<String, Object> params) {
        //查询商品列表
        IPage<SpuInfoEntity> page = baseDao.selectPage(
                getPage(params, "spu_id", true),
                getWrapper(params)
        );
        //获取[sys_user]的信息
        List<SysUserDTO> userList = sysUserService.getList();
        //将用户编号填充至creatorName&updaterName
        page.getRecords().forEach(entity -> {
            entity.setCreatorName(sysUserService.getUsernameById(entity.getCreator(), userList));
            entity.setUpdaterName(sysUserService.getUsernameById(entity.getUpdater(), userList));
        });
        return getPageData(page, SpuInfoDTO.class);
    }

    @Override
    public QueryWrapper<SpuInfoEntity> getWrapper(Map<String, Object> params) {
        String spuNo = (String) params.get("spuNo");
        String lotNo = (String) params.get("lotNo");
        String spuName = (String) params.get("spuName");
        String spuType = (String) params.get("spuType");
        String contType = (String) params.get("contType");
        String status = (String) params.get("status");

        QueryWrapper<SpuInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("spu_show", "0");
        wrapper.eq(StringUtils.isNotBlank(spuNo), "spu_no", spuNo);
        wrapper.eq(StringUtils.isNotBlank(lotNo), "lot_no", lotNo);
        wrapper.eq(StringUtils.isNotBlank(spuType), "spu_type", spuType);
        wrapper.eq(StringUtils.isNotBlank(contType), "cont_type", contType);
        wrapper.eq(StringUtils.isNotBlank(status), "status", status);
        wrapper.like(StringUtils.isNotBlank(spuName), "spu_name", spuName);

        return wrapper;
    }

    @Override
    public List<SpuInfoDTO> selectSpuList() {
        QueryWrapper<SpuInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("spu_show", "0");
        wrapper.eq("status", "0");
        List<SpuInfoEntity> entityList = baseDao.selectList(wrapper);
        return ConvertUtils.sourceToTarget(entityList, SpuInfoDTO.class);
    }

    @Override
    public void save(SpuInfoDTO dto) {
        String spuNo = dto.getSpuNo();
        if (StringUtils.isNotBlank(spuNo)) {
            SpuInfoEntity entity = selectBySpuNo(spuNo);
            if (entity != null) {
                throw new RootException("新增商品已存在!");
            }
        }
        super.save(dto);
    }

    @Override
    public SpuInfoEntity selectBySpuNo(String spuNo) {
        final QueryWrapper<SpuInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(spuNo), "spu_no", spuNo);
        return baseDao.selectOne(wrapper);
    }
}